package logic

import (
	"context"
	"errors"
	"math/rand"
	"time"
	"user-login-reg/model/cache"
	"user-login-reg/regrpc/internal/svc"
	"user-login-reg/regrpc/regrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 短信验证码
func (l *SendSmsLogic) SendSms(in *regrpc.SendSmsRequest) (*regrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	code := rand.Intn(9000) + 1000
	cache.Rdb.Incr(l.ctx, "sendsmsCode"+in.Account)

	resule, _ := cache.Rdb.Get(l.ctx, "sendsmsCode"+in.Account).Int64()
	if resule == 2 {
		return nil, errors.New("同一手机号每分钟仅允许发送1次短信验证码")
	}

	//pkg.SendSms(strconv.Itoa(code), in.Account)

	cache.Rdb.Set(l.ctx, "sendsms"+in.Account, code, time.Minute*3)

	return &regrpc.SendSmsResponse{}, nil
}
